﻿using System;


namespace Rogan.Common
{
    public class LogHelper
    {
       //private static log4net.ILog _logInfo = log4net.LogManager.GetLogger("logInfo");

        private static log4net.ILog _logError = log4net.LogManager.GetLogger("logError");

        private static log4net.ILog _logDebug = log4net.LogManager.GetLogger("logDebug");

        /// <summary>
        /// 记录提示信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteInfo(string info)
        {
            //_logInfo.Info(info);
            _logError.Error(new LogMessage { Title = "提示信息", Message = info,CategoryID = 1,OperationId = 1});
        }

        /// <summary>
        /// 记录提示信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="info"></param>
        public static void WriteInfo(string title,String info)
        {
            //_logInfo.Info(info);
            _logError.Error(new LogMessage { Title = title, Message = info, CategoryID = 1, OperationId = 1 });
        }
        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteError(string info, Exception ex)
        {
            string errorMsg = FormatException(ex);
            _logError.Error(new LogMessage { Title = info, Message = errorMsg, CategoryID = 1, OperationId = 1 });
        }

        public static void WriteLogMessage(LogMessage message)
        {
            _logError.Error(message);
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteDebug(string info)
        {
            _logDebug.Debug(info);
        }

        /// <summary>
        /// 美化错误信息
        /// </summary>
        /// <param name="ex">异常</param>
        /// <returns>错误信息</returns>
        public static string FormatException(Exception ex)
        {
            string errorMsg = string.Format("[异常类型]：{0} <br>[异常信息]：{1} <br>[堆栈调用]：{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
            errorMsg = errorMsg.Replace("\r\n", "<br>");
            errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>");
            return errorMsg;
        }
     
    }
}
